﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Clandestine.Entities;
using System.Drawing;
using Clandestine.Items;

namespace Clandestine
{
    public class MapPlayer
    {
        public AnimatedSprite Sprite = new AnimatedSprite();

        public Inventory Inventory = new Inventory();
        public List<Clandestine.Battle.Player> Party = new List<Clandestine.Battle.Player> {
                            new Clandestine.Battle.Player("player1", "battle_wizard.png"),
                            new Clandestine.Battle.Player("player2", "battle_thief.png"),
                            new Clandestine.Battle.Player("player3", "battle_fighter.png")
        };

        private float x, y;

        public float X
        {
            get { return x; }
            set
            {
                x = value;
                Sprite.X = (int)x;
            }
        }
        public float Y
        {
            get { return y; }
            set
            {
                y = value;
                Sprite.Y = (int)y;
            }
        }

        public int Width
        {
            get { return Sprite.UnscaledWidth; }
        }

        public int Height
        {
            get { return Sprite.UnscaledHeight; }
        }

        public PointF Position
        {
            get { return new PointF(X, Y); }
            set
            {
                X = value.X;
                Y = value.Y;
            }
        }

        public float Layer
        {
            get { return Sprite.Layer - (Entity.SpriteLayerDifference*2); }
            set { Sprite.Layer = value + (Entity.SpriteLayerDifference*2); } // difference * 2 so we're above entities on current layer.
        }

        public Size Size
        {
            get { return new Size(Width, Height); }
        }

        public MapPlayer()
        {
            // For now, just load this one.
            Sprite.AddAnimations("rpgmaker/mage.png", "sprite_metadata/npc_32x48.xml");
            this.Layer = 99f;

            // Nightmares of VB's IIf() coming back...
            Ticker.AddTimer((deltams)
                => tick((int)deltams,
                (Keyboard.IsPressed(Key.Left) ? -1 : Keyboard.IsPressed(Key.Right) ? 1 : 0),
                (Keyboard.IsPressed(Key.Up) ? -1 : Keyboard.IsPressed(Key.Down) ? 1 : 0)));
        }

        public Rectangle GetCollisionRectangle()
        {
            return GetCollisionRectangle(Point.Round(this.Position));
        }
        public Rectangle GetCollisionRectangle(Point position)
        {
            return new Rectangle(position.X, position.Y + (this.Height - 20), this.Width, 20);
        }

        private void tick(int deltams, int xDirection, int yDirection)
        {
            const float walkSpeed = (80f / 1000f);

            if (WorldMap.Paused)
                return;

            updateAnimation(xDirection, yDirection);

            PointF newPosition = new PointF((Position.X + (walkSpeed * deltams * xDirection)),
                    (Position.Y + (walkSpeed * deltams * yDirection)));

            Position = Collision.TryMove(Position, newPosition, Size);
        }

        private void updateAnimation(int xDirection, int yDirection)
        {
            // y animations
            if ((yDirection < 0) && (Sprite.CurrentAnimation.Name != "walk_up"))
                Sprite.SetAnimation("walk_up");
            if ((yDirection > 0) && (Sprite.CurrentAnimation.Name != "walk_down"))
                Sprite.SetAnimation("walk_down");

            // Only do x animations if there is no y!
            if (yDirection == 0)
                if ((xDirection > 0) && (Sprite.CurrentAnimation.Name != "walk_right"))
                    Sprite.SetAnimation("walk_right");
                else if ((xDirection < 0) && (Sprite.CurrentAnimation.Name != "walk_left"))
                    Sprite.SetAnimation("walk_left");

            // Make sure we handle stationary ones too.
            if ((xDirection == 0) && (yDirection == 0))
            {
                Sprite.Playing = false;
                Sprite.CurrentAnimation.CurrentFrame = 1; // stand still frame
            }
            else
                Sprite.Playing = true;
        }



        public void Dispose()
        {
            Sprite.Dispose();
        }
    }
}
